package engine.newUi
{
	import engine.control.TurnPapersViewGroup;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.RoleVo;
	import engine.flag.ResourcesPath;
	import engine.logic.login.PlayCDTimerLogic;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;
	import newx.com.control.window.NxWindowEvent;
	import newx.utils.MemoryOptimise;

	/**
	 *好友雇佣 
	 * @author yanghr
	 * 
	 */
	public class EngageFriendLogic
	{
		private static var _self:EngageFriendLogic;
		
		/**
		 *固有属性 
		 */		
		private const BG:String	=	"p_mc";
		private const NAME:String	=	"txt";
		private const LINK:String	=	"txt4";
		private const NONE_BTN:String	=	"bt_0";
		private const FIR_BTN:String	=	"bt_1";
		private const SEC_BTN:String	=	"bt_2";
		private const BACK_COLOR:String	=	"dt_mc";
		private const CONTAINER:String		=	"img01";
	
		private const MODEL_OBJ:String="Model";
		private const NAME_OBJ:String="name";
		private const STATE_OBJ:String="state";
		private const TIME_OBJ:String="time_left";
		private const DATA:String="data";
		
		private const IMG:String="img000";
		
		private const ENGAGE:String=StaticIni.getSwfLangStr("Common_Employ");
		private const CONVINCE:String=StaticIni.getSwfLangStr("游说");
		private const BLOODLE:String=StaticIni.getSwfLangStr("UIString_1000642");
		
		/**
		 *通信常量 
		 */		
		private const INDUSTRY:int=1;
		/**
		 *three state 
		 */		
		private const OTHER:int=1,MAIN:int=2,NEVER:int=0
		private var _ui:EngageFriendUI;
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		private var _groupPageTurner:TurnPapersViewGroup;
		private var _isInit:Boolean=false;
		
		private var _currentIndustry:int;
		
		private var _blindFlag:int=-1;
		
		private var engageFunc:Function;
		private var convinceFunc:Function;
		private var _otherArgs:Array
		private var _dataArr:Array;
		private var _dataMap:Object;
		public static function getInstance():EngageFriendLogic
		{
			if(_self==null)
			{
				_self=new EngageFriendLogic();
			}
			return _self;
		}
		public function get ui():EngageFriendUI
		{
			if(_ui==null)
			{
				_ui=new EngageFriendUI
			}
			return _ui;
		}
		public function EngageFriendLogic()
		{
		}
		public function Init():void
		{
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			
			_groupPageTurner    = new  TurnPapersViewGroup(ui.mainGroup,ui.preBtn,ui.nextBtn,showItem,removeItem,afterShowItem,ui.pageLab);
			MemoryOptimise.stopMovie(ui.mainGroup);
			PlayCDTimerLogic.getInstance().resgis(9,cdIt);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_REQUEST_ENGAGE_STATE),resetAll)
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_UPDATE_ENGAGE_STATE),upDataSingle)
			function removeItem(mc:MovieClip):void
			{
				if(mc[IMG])
				{
					mc[IMG].clear();
					mc[IMG].source=null;
				}
				mc[NAME+mc.currentFrame].text="";
				if(mc.currentFrame==3)
					mc[LINK].htmlText="";
				mc[DATA]=null;
			}
			function afterShowItem():void
			{
//				mc[BG].gotoAndStop(1);
			}
		}
		private function resetAll(e:CustomMsgEvent):void
		{
			trace("EngageFriendLogic.customMsg(e)",e.pList,e.pList.length);
			for (var i:int = 1; i <e.pList.length; i+=4) 
			{
				var obj:Object=new Object;
				obj[NAME_OBJ]=e.pList[i];
				obj[MODEL_OBJ]=e.pList[i+1];
				obj[STATE_OBJ]=e.pList[i+2];
				obj[TIME_OBJ]=e.pList[i+3];
				addObj(obj)
			}
			_ui.show();
		}
		private function upDataSingle(e:CustomMsgEvent):void
		{
			trace("EngageFriendLogic.upDataSingle(e)",e.pList);
			var obj:Object=new Object;
			obj[NAME_OBJ]=e.pList[0];
			obj[MODEL_OBJ]=e.pList[1];
			obj[STATE_OBJ]=e.pList[2];
			obj[TIME_OBJ]=e.pList[3];
			refreshSingle(obj);
			
		}
		private function addObj(obj:Object):void
		{
			_dataMap[obj[NAME_OBJ]]=obj;
			_dataArr.push(obj);
			if(!_isInit)
			{
				refresh()
			}
		}
		private function refreshSingle(obj:Object):void
		{
			if(_dataMap ==null)return;
			var oo:Object=_dataMap[obj[NAME_OBJ]];
			if(oo)
			{
				var index:int=_dataArr.indexOf(oo);
				_dataArr[index]=obj;
				_dataMap[obj[NAME_OBJ]]=obj
			}
			else
			{
				trace("Error~EngageFriendLogic.refreshSingle(obj)");
				
			}
			if(!_isInit)
			{
				refresh()
			}
			
		}
		private function mvGoto(mc:MovieClip,fram:int):void
		{
			mc.gotoAndStop(fram)
			switch(fram)
			{
				case 1:
				{
					mc["txt5"].htmlText=ENGAGE;
					mc["txt5"].mouseEnabled=false;
					break;
				}
				case 2:
				{
					mc["txt6"].htmlText=CONVINCE;
					mc["txt7"].htmlText=BLOODLE;
					mc["txt6"].mouseEnabled=mc["txt7"].mouseEnabled=false;
					break;
				}
				default:
				{
					break;
				}
			}
		}
		private function showItem(mc:MovieClip,data:Object):void
		{
			if(!mc[IMG])
			{
				mc[IMG]=new NxClip;
				mc[BG][CONTAINER].addChild(mc[IMG]);
				mc.gotoAndStop(1);
			}
			mc[DATA]=data;
//			mc.gotoAndStop(data[STATE_OBJ]+1)
			mvGoto(mc,data[STATE_OBJ]+1);
			mc[IMG].source=ResourcesPath.getHeadPic(data[MODEL_OBJ]);
			mc[NAME+mc.currentFrame].text=data[NAME_OBJ];
//			mc[BACK_COLOR].stop();
			if(mc.currentFrame==3)
				mc[LINK].htmlText=getTimeByFormat(data[TIME_OBJ],"hh小时mm分钟")//PlayCDTimerLogic.getInstance().getTimerStr(data[TIME_OBJ],true);
			mc.addEventListener(MouseEvent.CLICK,linkHandle)
		}
		private function linkHandle(e:MouseEvent):void
		{
			trace("EngageFriendLogic.linkHandle(e)",e.currentTarget);
			var mc:MovieClip=e.currentTarget as MovieClip;
			var data:Object=mc[DATA];
			var name:String=data[NAME_OBJ];
			var p:DisplayObject=e.target as DisplayObject
			if(mc[NONE_BTN]==(p))
			{
				engage(name)
			}
			else if(mc[FIR_BTN]==(p))
			{
				convince(name)
			}
			else if(mc[SEC_BTN]==(p))
			{
				boodle(name)
			}
			else
			{
				trace("EngageFriendLogic.linkHandle(e)",e.target);
			}
			
		}
		/**
		 *雇佣 
		 * @param name
		 * 
		 */		
		public function engage(name:String):void
		{
			trace("EngageFriendLogic.engage(name)");
			if(engageFunc!=null)
			{
				var arr:Array=[];
				arr=arr.concat(_currentIndustry,name,_otherArgs)
				engageFunc.apply(this,arr);
			}
			else
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_ENGAGE_PLAYER,new WString(name),_currentIndustry)
			}
		}
		/**
		 *游说 
		 * @param name
		 * 
		 */		
		public function convince(name:String):void
		{
			trace("EngageFriendLogic.convince(name)",name);
			if(convinceFunc!=null)
			{
				var arr:Array=[];
				arr=arr.concat(_currentIndustry,name,_otherArgs)
				convinceFunc.apply(this,arr);
//				convinceFunc(_currentIndustry,name)
			}
			else
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_LOBBY_PLAYER,new WString(name),_currentIndustry)
			}
		}
		/**
		 *贿赂 
		 * @param name
		 * 
		 */		
		public function boodle(name:String):void
		{
			trace("EngageFriendLogic.boodle(name)");
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_BRIBERYPRICE_PLAYER,new WString(name),_currentIndustry)
		}
		/**
		 * 
		 * @param industryID
		 * @param engage 雇佣
		 * 	function engage(id:int,name:String,..args):void
		 * @param convinceFunc 游说
		 * 	function convinceFunc(id:int,name:String,..args):void
		 * 
		 */		
		public function callForShow(industryID:int=1,engage:Function=null,convinceFunc:Function=null,...args):void
		{
			_currentIndustry=industryID;
			_isInit=true
			_dataArr=[];
			_dataMap=new Object
			_otherArgs=args;
			engageFunc	=	engage;
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_REQUEST_ENGAGE_STATE);
		}
		public function	BeforeShow():void
		{
			addAllEventListener();
			refresh();
			_isInit=false;
		}
		public function	AfterHide():void
		{
			engageFunc=convinceFunc=null;
			removeAllEventListener()
		}
		private function cdIt(val:int ,col:int):void
		{
			_ui.cdLab.htmlStr	="CD时间："+	getTimeByFormat(val,"mm分钟ss秒");
		}
		/**
		 * 
		 * @param second
		 * @param format 格式如（hh-mm-ss）hh代表小时，mm代表分钟，ss代表秒
		 * @return 
		 * 
		 */		
		public function getTimeByFormat(second:int,format:String):String
		{
			var h:int=int(second/3600);
			var m:int=int((second-3600*h)/60);
			var s:int=int(second-3600*h-60*m);
			return swap(swap(swap(format,h,"hh"),m,"mm"),s,"ss")
			function swap(mainStr:String,time:int,splider:String):String
			{
				var arr:Array=mainStr.split(splider);
				if(arr&&arr.length)
					return arr.join(time);
				return mainStr;
			
			}
		}
		private function upDataObj(targetObj:Object):void
		{
			var ovObj:Object=_dataMap[targetObj[NAME_OBJ]];
			var index:int=_dataArr.indexOf(ovObj);
			_dataArr[index]=ovObj;
		}
		private function refresh():void
		{
			_groupPageTurner.dataProvider=_dataArr;
		}
		private function onaddPerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			trace("EngageFriednLogic.onaddPerHandle(PropertiesName, PropertiesValue, data)",PropertiesName,PropertiesValue,data);
			var obj:Object=new Object;
			if(PropertiesValue&&PropertiesValue.length)
			{
				obj[MODEL_OBJ]=PropertiesValue[0];
				obj[NAME_OBJ]=PropertiesValue[1];
				obj[STATE_OBJ]=PropertiesValue[2];
			}
			_dataMap[obj[NAME_OBJ]]=obj;
			_dataArr[PropertiesName]=obj;
			if(!_isInit)
			{
				refresh()
			}
		}
		private function onremovePerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			trace("EngageFriednLogic.onremovePerHandle(PropertiesName, PropertiesValue, data)",PropertiesName,PropertiesValue,data);
			_dataArr.splice(PropertiesName,1);
			if(!_isInit)
			{
				refresh();
			}
			
		}
		public function addAllEventListener():void
		{
			_ui.addEventListener(NxWindowEvent.WINDOW_CLOSE,close,true);
			_groupPageTurner.addListener();
		}
		private function close(e:Event):void
		{
			_ui.hide();
		}
		public function removeAllEventListener():void
		{
			_ui.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close,true);
			_groupPageTurner.removeListener();
		}
	}
}